#!/usr/bin/env python3

'''
Author       : bopo
Date         : 2020-12-08 10:17:37
LastEditors  : bopo
LastEditTime : 2020-12-08 14:42:53
FilePath     : /stealer/stealer/adapter/xigua.py
Description  :
'''
import re

import requests

from stealer import config
from . import BaseAdapter

headers = {
    "accept": "*/*",
    "accept-encoding": "gzip, deflate, br",
    "accept-language": "en,zh-CN;q=0.9,zh;q=0.8,de;q=0.7",
    "user-agent": config.USER_AGENT
}

download_headers = {
    "accept": "*/*",
    "accept-encoding": "identity;q=1, *;q=0",
    "host": "jsmov2.a.yximgs.com",
    "range": "bytes=0-",
    "sec-fetch-dest": "video",
    "sec-fetch-mode": "no-cors",
    "sec-fetch-site": "cross-sit",
    "user-agent": config.USER_AGENT
}


class XiGuaAdapter(BaseAdapter):

    @classmethod
    def get_prefix_pattern(cls):
        return 'ixigua\.com\/'

    @classmethod
    def make_url(cls, index):
        return f'https://www.ixigua.com/{index}'

    def parse(self, url=None, header: bool = True):
        res = requests.get(url, headers=headers)
        url = re.findall(r"(?<=type=\"video\/mp4\" src=\")(.*?)(?=\")", res.text)[0]

        data = {
            'link': url,
            'name': 'name',
            'meta': {
                'headers': download_headers
            }}

        return data
